package com.KOL.model;

import java.awt.*;

import com.util.ImagesLoader;
import com.KOL.present.ChunkDisplay;

public class TiledEntity extends Entity {

  // TODO make octogonal
  public final static int NE = 0;
  public final static int SE = 1;
  public final static int SW = 2;
  public final static int NW = 3;
  public final static int STILL = 4;

  public final static int NUM_DIRS = 4;

  protected int xTile, yTile;
  protected ChunkDisplay currentChunk;


  public TiledEntity(int x, int y, int w, int h,
                           ImagesLoader imsLd, String name,
		                   ChunkDisplay currChunk) {
    super(0, 0, w, h, imsLd, name);
    setStep(0, 0);
    currentChunk = currChunk;

    if (!currentChunk.validTileLoc(x, y)) {
      System.err.println("Tile location (" + x + "," + y +
                                        ") not valid; using (0,0)");
      x = 0; y = 0;
    }
    xTile = x; yTile = y;
  }

  public void setTileLoc(Point pt) {
    xTile = pt.x;
    yTile = pt.y;
  }

  public Point getTileLoc()
  {  return new Point(xTile, yTile);  }

  public Point tryMove(int quad)
  /* The quadrant is translated into a tile coordinate, and tested
     for validity. The translation varies depending on if the row
     position of the curent tile (yTile) is even or odd.
     null is returned if the new position is invalid.
  */
  {
    Point nextPt;
    if (quad == NE)
      nextPt = (yTile%2 == 0)? new Point(xTile,yTile-1) : new Point(xTile+1,yTile-1);
    else if (quad == SE)
      nextPt = (yTile%2 == 0)? new Point(xTile,yTile+1) : new Point(xTile+1,yTile+1);
    else if (quad == SW)
      nextPt = (yTile%2 == 0)? new Point(xTile-1,yTile+1) : new Point(xTile,yTile+1);
    else if (quad == NW)
      nextPt = (yTile%2 == 0)? new Point(xTile-1,yTile-1) : new Point(xTile,yTile-1);
    else
      return null;

    if (currentChunk.validTileLoc(nextPt.x, nextPt.y))
      return nextPt;
    else
     return null;
  }

  protected ChunkDisplay getCurrentChunk() {
      return currentChunk;
  }

  protected void setCurrentChunk(ChunkDisplay chunk) {
      currentChunk = chunk;
  }

  public int getRandDirection()
  {   return (int)(NUM_DIRS * Math.random());  }


  public int whichQuadrant(Point p)
  /* Relative to this sprite's tile position, which quadrant direction
     is needed to get to p? p is assumed to be adjacent to this tile.
     whichQuadrant() return -1 if there is no suitable direction.
  */
  { // System.out.println("tile: " + xTile + ", " + yTile);
    // System.out.println("to: " + p.x + ", " + p.y);

    if ((xTile == p.x) && (yTile == p.y))
      return STILL;   // the sprite is already on tile p

    if (yTile%2 == 0) {    // the current tile is on an even row
      if ((xTile == p.x) && (yTile-1 == p.y))
        return NE;
      if ((xTile == p.x) && (yTile+1 == p.y))
        return SE;
      if ((xTile-1 == p.x) && (yTile+1 == p.y))
        return SW;
      if ((xTile-1 == p.x) && (yTile-1 == p.y))
        return NW;
    }
    else {    // the current tile is on an odd row
      if ((xTile+1 == p.x) && (yTile-1 == p.y))
        return NE;
      if ((xTile+1 == p.x) && (yTile+1 == p.y))
        return SE;
      if ((xTile == p.x) && (yTile+1 == p.y))
        return SW;
      if ((xTile == p.x) && (yTile-1 == p.y))
        return NW;
    }
    return -1;
  }

}
